--[[

This is a small addon designed to help with the issue of all the random items collected while on instance runs.

It will sort a specified pack by a specified sort method.

Current sort methods are by vendor price, or by name.
]]

---------------------------------------------------

function LPS_OnLoad( frame )
	-- Register the events we care about
	frame:RegisterEvent("ADDON_LOADED");
	frame:RegisterEvent("PLAYER_ENTERING_WORLD");
	frame:RegisterEvent("PLAYER_LEAVING_WORLD");

	-- Register our slash commands
	SLASH_PACKSORT1 = LPS_SLASH1;
	SLASH_PACKSORT2 = LPS_SLASH2;
	SlashCmdList["PACKSORT"] = function(msg)
		LPS_SlashCommandHandler(msg);
	end
end

function LPS_OnEvent( frame, event, ... )
	local LPS_DebugEvents	= false;
	local curTime;

	if (LPS_DebugEvents == true) then
		DEFAULT_CHAT_FRAME:AddMessage("LPS Debug: Event Fired! Event is "..event);

		-- Print all of the function's arguments (those passed to the ellipsis (...) only) to the default chat frame.
		local debugmsg;
		for i = 1, select('#', ...)
		do
			msg = select(i, ...);
			DEFAULT_CHAT_FRAME:AddMessage("Event Argument "..i..": "..select(i, ...));
		end
		
		DEFAULT_CHAT_FRAME:AddMessage("Last Time was "..LPS_Time.." current Time is "..curTime);
	end
	
	if ((event == "ADDON_LOADED") and (select(1, ...) == "LysPackSort")) then
		LPS_DataDefine();
		LPS_ENABLE	= false;
		LPS_GBSort	= false;
		LPS_BankOpen	= false;
		LPS_RegConfig();
		return;

	elseif (event == "PLAYER_ENTERING_WORLD") then
		--frame:RegisterEvent("BAG_UPDATE");
		--frame:RegisterEvent("BAG_UPDATE_COOLDOWN");
		--frame:RegisterEvent("ITEM_LOCK_CHANGED");
		--frame:RegisterEvent("ITEM_UNLOCKED");
		frame:RegisterEvent("MERCHANT_SHOW");
		frame:RegisterEvent("MERCHANT_CLOSED");
		frame:RegisterEvent("BANKFRAME_OPENED");
		frame:RegisterEvent("BANKFRAME_CLOSED");
		frame:RegisterEvent("MAIL_SHOW");
		frame:RegisterEvent("MAIL_CLOSED");
		frame:RegisterEvent("AUCTION_HOUSE_SHOW");
		frame:RegisterEvent("AUCTION_HOUSE_CLOSED");
		frame:RegisterEvent("PLAYER_ENTER_COMBAT");
		frame:RegisterEvent("PLAYER_LEAVE_COMBAT");
		--frame:RegisterEvent("GUILDBANKBAGSLOTS_CHANGED");
		frame:RegisterEvent("GUILDBANKFRAME_CLOSED");
		--frame:RegisterEvent("UI_ERROR_MESSAGE");
		return;

	elseif (event == "PLAYER_LEAVING_WORLD") then
		--frame:UnregisterEvent("BAG_UPDATE");
		--frame:UnregisterEvent("BAG_UPDATE_COOLDOWN");
		--frame:UnregisterEvent("ITEM_LOCK_CHANGED");
		--frame:UnregisterEvent("ITEM_UNLOCKED");
		frame:UnregisterEvent("MERCHANT_SHOW");
		frame:UnregisterEvent("MERCHANT_CLOSED");
		frame:UnregisterEvent("BANKFRAME_OPENED");
		frame:UnregisterEvent("BANKFRAME_CLOSED");
		frame:UnregisterEvent("MAIL_SHOW");
		frame:UnregisterEvent("MAIL_CLOSED");
		frame:UnregisterEvent("AUCTION_HOUSE_SHOW");
		frame:UnregisterEvent("AUCTION_HOUSE_CLOSED");
		frame:UnregisterEvent("PLAYER_ENTER_COMBAT");
		frame:UnregisterEvent("PLAYER_LEAVE_COMBAT");
		--frame:UnregisterEvent("GUILDBANKBAGSLOTS_CHANGED");
		frame:UnregisterEvent("GUILDBANKFRAME_CLOSED");
		--frame:UnregisterEvent("UI_ERROR_MESSAGE");
		return;

--[[
	elseif ((event == "MERCHANT_SHOW")
		or (event == "MAIL_SHOW")
		or (event == "AUCTION_HOUSE_SHOW")
		or (event == "PLAYER_ENTER_COMBAT")
		) then
		--frame:UnregisterEvent("BAG_UPDATE");
		return;

	elseif ((event == "MERCHANT_CLOSED")
		or (event == "MAIL_CLOSED")
		or (event == "AUCTION_HOUSE_CLOSED")
		or (event == "PLAYER_LEAVE_COMBAT")
		) then
		--frame:RegisterEvent("BAG_UPDATE");
		if (LPS_ENABLE) then
			LPS_SortPack();
		elseif (LPS_Config.autosort) then
			LPS_PM_AutoSort();
		end
		return;
]]

	elseif (event == "BANKFRAME_OPENED") then
		LPS_BankOpen	= true;
		LPS_ENABLE	= false;  -- This stops an active sort if it is running when the bank is opened.
		return;

	elseif (event == "BANKFRAME_CLOSED") then
		LPS_BankOpen	= false;
		LPS_ENABLE	= false;  -- This stops an active sort if it is running when the bank is closed.
		return;

	elseif (event == "GUILDBANKFRAME_CLOSED") then
		-- Stop all active sorts when the guild bank closes.
		LPS_GBSort	= false;
		LPS_ENABLE	= false;
		return;
	end

end

function LPS_OnUpdate(self, elapsed)
	local curTime = GetTime();

	if (curTime >= LPS_DelayTime) then
		if (LPS_ENABLE) then
			LPS_SortPack();
		elseif (LPS_Config.autosort) then
			LPS_PM_AutoSort();
		end

		if (LPS_GBSort) then
			if (LPS_GB_PauseSort == false) then
				LPS_SortGBTab();
			end
		end
		LPS_DelayTime = curTime + LPS_UpdateInterval;
	end
end


function LPS_SlashCommandHandler(msg)
	local value;
	local myStyle;
	
	msg = string.lower(msg);

	if (msg == '') then msg = nil end

	if (msg) then
		-- Config Screen
		if (msg == LPS_CMD_CONFIG) then
			InterfaceOptionsFrame_OpenToCategory(LPS_GUI);
			return;

		-- Actually run the sort.
		elseif (msg == LPS_CMD_SORT) then
			LPS_ENABLE		= true;
			LPS_CURRENT_PACK	= 0;
			DEFAULT_CHAT_FRAME:AddMessage(LPS_MSG_SORTSTART);
			LPS_SortPack();
			return;

		-- Actually run the sort.
		elseif (msg == "defaults") then
			LPS_DataDefaults();
			return;

		-- Activate/De-activate auto-sort
		elseif (string.find(msg, LPS_CMD_AUTOSORT)) then
			if (LPS_Config.autosort) then
				LPS_Config.autosort = false;
				DEFAULT_CHAT_FRAME:AddMessage(LPS_MSG_AUTOSORTOFF);
			else
				LPS_Config.autosort = true;
				DEFAULT_CHAT_FRAME:AddMessage(LPS_MSG_AUTOSORTON);
			end
			return;
		end
	end

	local index = 0;
	local value = getglobal("LPS_HELP_TEXT"..index);
	while( value ) do
		DEFAULT_CHAT_FRAME:AddMessage(value, 0.2, 1.0, 0.2);
		index = index + 1;
		value = getglobal("LPS_HELP_TEXT"..index);
	end
end
